<?php
/**
 * 登录、注册
 */
if(!session_id()) session_start();

class Api_Login extends PhalApi_Api
{
	
	public function getRules()
	{
		return array(
			'userLogin'          => array(
				'country_code' => array('name' => 'country_code', 'type' => 'int', 'default' => '86', 'require' => true, 'desc' => '国家代号'),
				'user_login'   => array('name' => 'user_login', 'type' => 'string', 'require' => true, 'min' => '6', 'max' => '30', 'desc' => '账号'),
				'user_pass'    => array('name' => 'user_pass', 'type' => 'string', 'require' => true, 'min' => '1', 'max' => '30', 'desc' => '密码'),
			),
			'userReg'            => array(
				'country_code' => array('name' => 'country_code', 'type' => 'int', 'default' => '86', 'require' => true, 'desc' => '国家代号'),
				'user_login'   => array('name' => 'user_login', 'type' => 'string', 'require' => true, 'min' => '6', 'max' => '30', 'desc' => '账号'),
				'user_pass'    => array('name' => 'user_pass', 'type' => 'string', 'require' => true, 'min' => '1', 'max' => '30', 'desc' => '密码'),
				'user_pass2'   => array('name' => 'user_pass2', 'type' => 'string', 'require' => true, 'min' => '1', 'max' => '30', 'desc' => '确认密码'),
				'code'         => array('name' => 'code', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '验证码'),
				'source'       => array('name' => 'source', 'type' => 'string', 'default' => 'pc', 'desc' => '来源设备'),
				'source_type'  => array('name' => 'source_type', 'type' => 'int', 'default' => '0', 'desc' => '0：直播demo；1：小程序'),
			),
			'mobileLogin'        => array(
				'country_code' => array('name' => 'country_code', 'type' => 'int', 'default' => '86', 'require' => true, 'desc' => '国家代号'),
				'user_login'   => array('name' => 'user_login', 'type' => 'string', 'require' => true, 'min' => '6', 'max' => '30', 'desc' => '手机账号'),
				'code'         => array('name' => 'code', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '验证码'),
				'source'       => array('name' => 'source', 'type' => 'string', 'default' => 'pc', 'desc' => '来源设备'),
				'agent_code'   => array('name' => 'agent_code', 'type' => 'string', 'default' => '', 'desc' => '上级推荐码'),
			),
			'userFindPass'       => array(
				'country_code' => array('name' => 'country_code', 'type' => 'int', 'default' => '86', 'require' => true, 'desc' => '国家代号'),
				'user_login'   => array('name' => 'user_login', 'type' => 'string', 'require' => true, 'min' => '6', 'max' => '30', 'desc' => '账号'),
				'user_pass'    => array('name' => 'user_pass', 'type' => 'string', 'require' => true, 'min' => '1', 'max' => '30', 'desc' => '密码'),
				'user_pass2'   => array('name' => 'user_pass2', 'type' => 'string', 'require' => true, 'min' => '1', 'max' => '30', 'desc' => '确认密码'),
				'code'         => array('name' => 'code', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '验证码'),
				'source_type'  => array('name' => 'source_type', 'type' => 'int', 'default' => '0', 'desc' => '0：直播demo；1：小程序'),
			),
			'userLoginByThird'   => array(
				'openid'       => array('name' => 'openid', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '第三方openid'),
				'type'         => array('name' => 'type', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '第三方标识 qq/wx'),
				'nicename'     => array('name' => 'nicename', 'type' => 'string', 'default' => '', 'desc' => '第三方昵称'),
				'avatar'       => array('name' => 'avatar', 'type' => 'string', 'default' => '', 'desc' => '第三方头像'),
				'sign'         => array('name' => 'sign', 'type' => 'string', 'default' => '', 'desc' => '签名'),
				'source'       => array('name' => 'source', 'type' => 'string', 'default' => 'pc', 'desc' => '来源设备'),
				'access_token' => array('name' => 'access_token', 'type' => 'string', 'require' => true, 'desc' => '三方接口调用凭证'),
			),
			'getCode'            => array(
				'country_code' => array('name' => 'country_code', 'type' => 'int', 'default' => '86', 'require' => true, 'desc' => '国家代号'),
				'mobile'       => array('name' => 'mobile', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '手机号'),
				'sign'         => array('name' => 'sign', 'type' => 'string', 'default' => '', 'desc' => '签名'),
			),
			'getLoginCode'       => array(
				'country_code' => array('name' => 'country_code', 'type' => 'int', 'default' => '86', 'require' => true, 'desc' => '国家代号'),
				'mobile'       => array('name' => 'mobile', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '手机号'),
				'sign'         => array('name' => 'sign', 'type' => 'string', 'default' => '', 'desc' => '签名'),
			),
			'getForgetCode'      => array(
				'country_code' => array('name' => 'country_code', 'type' => 'int', 'default' => '86', 'require' => true, 'desc' => '国家代号'),
				'mobile'       => array('name' => 'mobile', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '手机号'),
				'sign'         => array('name' => 'sign', 'type' => 'string', 'default' => '', 'desc' => '签名'),
			),
			'getUnionid'         => array(
				'code' => array('name' => 'code', 'type' => 'string', 'desc' => '微信code'),
			),
			'logout'             => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
			),
			'getCancelCondition' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
			),
			'cancelAccount'      => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
				'time'  => array('name' => 'time', 'type' => 'string', 'desc' => '时间戳'),
				'sign'  => array('name' => 'sign', 'type' => 'string', 'desc' => '签名'),
			),
			'getCountrys'        => array(
				'field' => array('name' => 'field', 'type' => 'string', 'default' => '', 'desc' => '搜索json串'),
			),
		);
	}
	
	/**
	 * 会员登陆 需要密码
	 * @desc 用于用户登陆信息
	 * @return int code 操作码，0表示成功
	 * @return array info 用户信息
	 * @return string info[0].id 用户ID
	 * @return string info[0].user_nickname 昵称
	 * @return string info[0].avatar 头像
	 * @return string info[0].avatar_thumb 头像缩略图
	 * @return string info[0].sex 性别
	 * @return string info[0].signature 签名
	 * @return string info[0].coin 用户余额
	 * @return string info[0].login_type 注册类型
	 * @return string info[0].level 等级
	 * @return string info[0].province 省份
	 * @return string info[0].city 城市
	 * @return string info[0].birthday 生日
	 * @return string info[0].token 用户Token
	 * @return string info[0].usersig 腾讯IM签名
	 * @return string msg 提示信息
	 */
	public function userLogin()
	{
		$country_code = checkNull($this->country_code);
		$user_login   = checkNull($this->user_login);
		$user_pass    = checkNull($this->user_pass);
		
		$domain = new Domain_Login();
		$info   = $domain->userLogin($country_code, $user_login, $user_pass);
		if($info == 1001){
			return $this->fail('账号或密码错误!', 1001);
		} elseif($info == 1002){
			//禁用信息
			$baninfo = $domain->getUserban($user_login);
			
			return $this->fail('账号或密码错误!', 1002, [0 => $baninfo]);
		} elseif($info == 1003){
			return $this->fail('该账号已被禁用!', 1003);
		} elseif($info == 1004){
			return $this->fail('该账号已注销!', 1004);
		} elseif($info == 1005){
			return $this->fail('请先下麦再登录!', 1005);
		}
		
		return $this->success(['0' => $info]);
	}
	
	
	/**
	 * 会员注册
	 * @desc 用于用户注册信息
	 * @return int code 操作码，0表示成功
	 * @return array info 用户信息
	 * @return string info[0].id 用户ID
	 * @return string info[0].user_nickname 昵称
	 * @return string info[0].avatar 头像
	 * @return string info[0].avatar_thumb 头像缩略图
	 * @return string info[0].sex 性别
	 * @return string info[0].signature 签名
	 * @return string info[0].coin 用户余额
	 * @return string info[0].login_type 注册类型
	 * @return string info[0].level 等级
	 * @return string info[0].province 省份
	 * @return string info[0].city 城市
	 * @return string info[0].birthday 生日
	 * @return string info[0].token 用户Token
	 * @return string msg 提示信息
	 */
	public function userReg()
	{
		$country_code = checkNull($this->country_code);
		$user_login   = checkNull($this->user_login);
		$user_pass    = checkNull($this->user_pass);
		$user_pass2   = checkNull($this->user_pass2);
		$source       = checkNull($this->source);
		$code         = checkNull($this->code);
		$source_type  = checkNull($this->source_type);
		if($source_type != '1'){
			if(!$_SESSION['reg_mobile'] || !$_SESSION['reg_mobile_code']){
				return $this->fail('请先获取验证码!', 1001);
			}
			if($country_code != $_SESSION['country_code']){
				return $this->fail('国家不一致!', 1001);
			}
			if($user_login != $_SESSION['reg_mobile']){
				return $this->fail('手机号码不一致!', 1001);
			}
			if($code != $_SESSION['reg_mobile_code']){
				return $this->fail('验证码错误!', 1002);
			}
		}
		if($user_pass != $user_pass2){
			return $this->fail('两次输入的密码不一致!', 1003);
		}
		
		$check = passcheck($user_pass);
		if(!$check){
			return $this->fail('密码为6-20位字母数字组合!', 1004);
		}
		
		$domain = new Domain_Login();
		$info   = $domain->userReg($country_code, $user_login, $user_pass, $source);
		if($info == 1006){
			return $this->fail('密码为6-该手机号已被注册!', 1006);
		} elseif($info == 1007){
			return $this->fail('注册失败，请重试!', 1007);
		}
		
		$_SESSION['reg_mobile']            = '';
		$_SESSION['reg_mobile_code']       = '';
		$_SESSION['reg_mobile_expiretime'] = '';
		
		return $this->success([0 => $info]);
	}
	
	/**
	 * 会员登录
	 * @desc 用于用户注册信息
	 * @return int code 操作码，0表示成功
	 * @return array info 用户信息
	 * @return string info[0].id 用户ID
	 * @return string info[0].user_nickname 昵称
	 * @return string info[0].avatar 头像
	 * @return string info[0].avatar_thumb 头像缩略图
	 * @return string info[0].sex 性别
	 * @return string info[0].signature 签名
	 * @return string info[0].coin 用户余额
	 * @return string info[0].login_type 注册类型
	 * @return string info[0].level 等级
	 * @return string info[0].province 省份
	 * @return string info[0].city 城市
	 * @return string info[0].birthday 生日
	 * @return string info[0].token 用户Token
	 * @return string msg 提示信息
	 */
	public function mobileLogin()
	{
		$country_code = checkNull($this->country_code);
		$user_login   = checkNull($this->user_login);
		$code         = checkNull($this->code);
		$source       = checkNull($this->source);
		$agent_code   = checkNull($this->agent_code);
		if(!$_SESSION['login_mobile'] || !$_SESSION['login_mobile_code']){
			return $this->fail('请先获取验证码!', 1001);
		}
		if($country_code != $_SESSION['country_code']){
			return $this->fail('国家不一致!', 1001);
		}
		if($user_login != $_SESSION['login_mobile']){
			return $this->fail('手机号码不一致!', 1001);
		}
		if($code != $_SESSION['login_mobile_code']){
			return $this->fail('验证码错误!', 1002);
		}
		
		$domain = new Domain_Login();
		$info   = $domain->userLogin($country_code, $user_login, '', $agent_code);
		
		// session 处理
		$_SESSION['login_mobile']            = '';
		$_SESSION['login_mobile_code']       = '';
		$_SESSION['login_mobile_expiretime'] = '';
		
		// 如果用户不存在 则重新注册
		if($info == 1001){
			$domain = new Domain_Login();
			$info   = $domain->userReg($country_code, $user_login, '123456', $source, $agent_code);
			if($info == 1006){
				return $this->fail('密码为6-该手机号已被注册!', 1006);
			} elseif($info == 1007){
				return $this->fail('注册失败，请重试!', 1007);
			}elseif($info == 2002){
				return $this->fail('邀请码错误！', $info);
			}
			
			return $this->success(['0' => $info]);
		} elseif($info == 1002){
			//禁用信息
			$baninfo = $domain->getUserban($user_login);
			
			return $this->fail('账号或密码错误!', 1002, [0 => $baninfo]);
		} elseif($info == 1003){
			return $this->fail('该账号已被禁用!', 1003);
		} elseif($info == 1004){
			return $this->fail('该账号已注销!', 1004);
		} elseif($info == 1005){
			return $this->fail('请先下麦再登录!', 1005);
		}elseif($info == 2001){
			return $this->fail('你已经设置邀请码！', $info);
		} elseif($info == 2002){
			return $this->fail('邀请码错误！', $info);
		} elseif($info == 2003){
			return $this->fail('不能填写自己的邀请码！', $info);
		} elseif($info == 2004){
			return $this->fail('您填写的邀请码是该用户的上级！', $info);
		}
		
		return $this->success(['0' => $info]);
	}
	
	// 批量注册
	public function sds_batch()
	{
		for($num = 13123456781; $num <= 13123456789; $num++){
			$domain = new Domain_Login();
			$info   = $domain->userReg(86, $num, '123456', 'pc');
		}
		echo '完成';
	}
	
	/**
	 * 会员找回密码
	 * @desc 用于会员找回密码
	 * @return int code 操作码，0表示成功，1表示验证码错误，2表示用户密码不一致,3短信手机和登录手机不一致 4、用户不存在 801 密码6-12位数字与字母
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function userFindPass()
	{
		$country_code = checkNull($this->country_code);
		$user_login   = checkNull($this->user_login);
		$user_pass    = checkNull($this->user_pass);
		$user_pass2   = checkNull($this->user_pass2);
		$code         = checkNull($this->code);
		$source_type  = checkNull($this->source_type);//0:直播demo；1：小程序
		if($source_type != '1'){
			if(!$_SESSION['forget_country_code'] || !$_SESSION['forget_mobile'] || !$_SESSION['forget_mobile_code']){
				return $this->fail('请先获取验证码!', 1001);
			}
			if($country_code != $_SESSION['forget_country_code']){
				return $this->fail('国家不一致!', 1001);
			}
			if($user_login != $_SESSION['forget_mobile']){
				return $this->fail('手机号码不一致!', 1001);
			}
			if($code != $_SESSION['forget_mobile_code']){
				return $this->fail('验证码错误!', 1002);
			}
		}
		if($user_pass != $user_pass2){
			return $this->fail('两次输入的密码不一致!', 1003);
		}
		
		$check = passcheck($user_pass);
		if(!$check){
			return $this->fail('密码为6-20位字母数字组合!', 1004);
		}
		
		$domain = new Domain_Login();
		$info   = $domain->userFindPass($country_code, $user_login, $user_pass);
		if($info == 1006){
			return $this->fail('该帐号不存在!', 1006);
		} elseif($info === false){
			return $this->fail('重置失败，请重试!', 1007);
		}
		
		$_SESSION['forget_mobile_code']       = '';
		$_SESSION['forget_mobile']            = '';
		$_SESSION['forget_mobile_code']       = '';
		$_SESSION['forget_mobile_expiretime'] = '';
		
		return $this->success(['0' => $info]);
	}
	
	/**
	 * 第三方登录
	 * @desc 用于用户登陆信息
	 * @return int code 操作码，0表示成功
	 * @return array info 用户信息
	 * @return string info[0].id 用户ID
	 * @return string info[0].user_nickname 昵称
	 * @return string info[0].avatar 头像
	 * @return string info[0].avatar_thumb 头像缩略图
	 * @return string info[0].sex 性别
	 * @return string info[0].signature 签名
	 * @return string info[0].coin 用户余额
	 * @return string info[0].login_type 注册类型
	 * @return string info[0].level 等级
	 * @return string info[0].province 省份
	 * @return string info[0].city 城市
	 * @return string info[0].birthday 生日
	 * @return string info[0].token 用户Token
	 * @return string info[0].usersig 腾讯IM签名
	 * @return string msg 提示信息
	 */
	public function userLoginByThird()
	{
		$openid       = checkNull($this->openid);
		$type         = checkNull($this->type);
		$nicename     = checkNull($this->nicename);
		$avatar       = checkNull($this->avatar);
		$source       = checkNull($this->source);
		$sign         = checkNull($this->sign);
		$access_token = checkNull($this->access_token);
		$checkdata    = array('openid' => $openid);
		$issign       = checkSign($checkdata, $sign);
		if(!$issign){
			return $this->fail('签名错误!', 1001);
		}
		
		if($access_token){
			if($type == 'wx'){
				$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid;
				//file_put_contents(API_ROOT.'/../log/phalapi/login_userLoginByThird_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' wx access_token:'.json_encode($access_token)."\r\n",FILE_APPEND);
				//file_put_contents(API_ROOT.'/../log/phalapi/login_userLoginByThird_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' wx openid:'.json_encode($openid)."\r\n",FILE_APPEND);
				
				$res = $this->checkThirdUserInfo($url, $type);
				//file_put_contents(API_ROOT.'/../log/phalapi/login_userLoginByThird_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 三方信息wx验证结果:'.json_encode($res)."\r\n\r\n",FILE_APPEND);
				
				if($res['code'] != 0){
					return $this->fail('信息验证失败!', 0);
				}
			} elseif($type == 'qq'){
				$configpri = getConfigPri();
				$url       = "https://graph.qq.com/user/get_user_info?access_token=".$access_token."&oauth_consumer_key=".$configpri['login_qq_appid']."&openid=".$openid;
				//file_put_contents(API_ROOT.'/../log/phalapi/login_userLoginByThird_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' qq access_token:'.json_encode($access_token)."\r\n",FILE_APPEND);
				//file_put_contents(API_ROOT.'/../log/phalapi/login_userLoginByThird_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' qq openid:'.json_encode($openid)."\r\n",FILE_APPEND);
				
				$res = $this->checkThirdUserInfo($url, $type);
				//file_put_contents(API_ROOT.'/../log/phalapi/login_userLoginByThird_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 三方信息qq验证结果:'.json_encode($res)."\r\n",FILE_APPEND);
				
				if($res['code'] != 0){
					return $this->fail('信息验证失败!', 0);
				}
			}
		}
		
		$domain = new Domain_Login();
		$info   = $domain->userLoginByThird($openid, $type, $nicename, $avatar, $source);
		if($info == 1002){
			//禁用信息
			$baninfo = $domain->getThirdUserban($openid, $type);
			
			return $this->fail('禁用信息!', 1002, [0 => $baninfo]);
		} elseif($info == 1003){
			return $this->fail('该账号已被禁用!', 1003);
		} elseif($info == 1004){
			return $this->fail('该账号已注销!', 1004);
		} elseif($info == 1005){
			return $this->fail('请先下麦再登录!', 1005);
		}
		
		return $this->success([0 => $info]);
	}
	
	/**
	 * 获取登录短信验证码
	 * @desc 用于注册获取短信验证码
	 * @return int code 操作码，0表示成功,2发送失败
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function getLoginCode()
	{
		$rs           = array('code' => 0, 'msg' => '发送成功', 'info' => array(), "verificationcode" => 0);
		$country_code = checkNull($this->country_code);
		$mobile       = checkNull($this->mobile);
		$sign         = checkNull($this->sign);
		$sms_check    = $this->checkSmsType($country_code, $mobile);
		if($sms_check['code'] != 0){
			return $sms_check;
		}
		
		$checkdata = array('mobile' => $mobile);
		$issign    = checkSign($checkdata, $sign);
		if(!$issign){
			//return $this->fail('签名错误!', 1001);
		}
		
		$where     = "country_code='{$country_code}' and user_login='{$mobile}'";
		$checkuser = checkUser($where);
		/*if(empty($checkuser)){
			return $this->fail('该手机号暂未注册!', 1004);
		}*/
		
		/* if($_SESSION['country_code']==$country_code && $_SESSION['reg_mobile']==$mobile && $_SESSION['reg_mobile_expiretime']> time() ){
			$rs['code']=1002;
			$rs['msg']='验证码5分钟有效，请勿多次发送';
			return $rs;
		} */
		
		$limit = ip_limit();
		if($limit == 1){
			return $this->fail('您当日已发送次数过多!', 1003);
		}
		
		$mobile_code = random(6, 1);
		if(in_array($mobile, ['18086843363'])){
			$mobile_code = '123456';
		}
		/* 发送验证码 */
		$result = sendCode($country_code, $mobile, $mobile_code);
		if($result['code'] == 0){
			$rs['verificationcode']              = $mobile_code;
			$_SESSION['country_code']            = $country_code;
			$_SESSION['login_mobile']            = $mobile;
			$_SESSION['login_mobile_code']       = $mobile_code;
			$_SESSION['login_mobile_expiretime'] = time() + 60 * 5;
		} elseif($result['code'] == 667){
			$_SESSION['country_code']            = $country_code;
			$_SESSION['login_mobile']            = $mobile;
			$_SESSION['login_mobile_code']       = $result['msg'];
			$_SESSION['login_mobile_expiretime'] = time() + 60 * 5;
			
			$rs['verificationcode'] = '123456';
			$rs['code']             = 1002;
			$rs['msg']              = '验证码为：'.$result['msg'];
		} else{
			$rs['code'] = 1002;
			$rs['msg']  = $result['msg'];
		}
		
		return $rs;
	}
	
	/**
	 * 获取注册短信验证码
	 * @desc 用于注册获取短信验证码
	 * @return int code 操作码，0表示成功,2发送失败
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function getCode()
	{
		$rs           = array('code' => 0, 'msg' => '发送成功', 'info' => array(), "verificationcode" => 0);
		$country_code = checkNull($this->country_code);
		$mobile       = checkNull($this->mobile);
		$sign         = checkNull($this->sign);
		$sms_check    = $this->checkSmsType($country_code, $mobile);
		if($sms_check['code'] != 0){
			return $sms_check;
		}
		
		$checkdata = array('mobile' => $mobile);
		$issign    = checkSign($checkdata, $sign);
		if(!$issign){
			return $this->fail('签名错误!', 1001);
		}
		
		$where     = "country_code='{$country_code}' and user_login='{$mobile}'";
		$checkuser = checkUser($where);
		if($checkuser){
			return $this->fail('该手机号已注册!', 1004);
		}
		
		/* if($_SESSION['country_code']==$country_code && $_SESSION['reg_mobile']==$mobile && $_SESSION['reg_mobile_expiretime']> time() ){
			$rs['code']=1002;
			$rs['msg']='验证码5分钟有效，请勿多次发送';
			return $rs;
		} */
		
		$limit = ip_limit();
		if($limit == 1){
			return $this->fail('您当日已发送次数过多!', 1003);
		}
		
		$mobile_code = random(6, 1);
		/* 发送验证码 */
		$result = sendCode($country_code, $mobile, $mobile_code);
		if($result['code'] == 0){
			$rs['verificationcode']            = $mobile_code;
			$_SESSION['country_code']          = $country_code;
			$_SESSION['reg_mobile']            = $mobile;
			$_SESSION['reg_mobile_code']       = $mobile_code;
			$_SESSION['reg_mobile_expiretime'] = time() + 60 * 5;
		} elseif($result['code'] == 667){
			$_SESSION['country_code']          = $country_code;
			$_SESSION['reg_mobile']            = $mobile;
			$_SESSION['reg_mobile_code']       = $result['msg'];
			$_SESSION['reg_mobile_expiretime'] = time() + 60 * 5;
			
			$rs['verificationcode'] = '123456';
			$rs['code']             = 1002;
			$rs['msg']              = '验证码为：'.$result['msg'];
		} else{
			$rs['code'] = 1002;
			$rs['msg']  = $result['msg'];
		}
		
		return $rs;
	}
	
	/**
	 * 获取找回密码短信验证码
	 * @desc 用于找回密码获取短信验证码
	 * @return int code 操作码，0表示成功,2发送失败
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function getForgetCode()
	{
		$rs           = array('code' => 0, 'msg' => '发送成功', 'info' => array(), "verificationcode" => 0);
		$country_code = checkNull($this->country_code);
		$mobile       = checkNull($this->mobile);
		$sign         = checkNull($this->sign);
		$sms_check    = $this->checkSmsType($country_code, $mobile);
		if($sms_check['code'] != 0){
			return $sms_check;
		}
		
		$checkdata = array('mobile' => $mobile);
		$issign    = checkSign($checkdata, $sign);
		if(!$issign){
			return $this->fail('签名错误!', 1001);
		}
		
		$where     = "country_code='{$country_code}' and user_login='{$mobile}'";
		$checkuser = checkUser($where);
		if(!$checkuser){
			return $this->fail('该手机号未注册!', 1004);
		}
		
		//判断手机号是否注销
		$is_destroy = checkIsDestroyByLogin($country_code, $mobile);
		if($is_destroy){
			return $this->fail('该手机号已注销!', 1005);
		}
		
		if($_SESSION['forget_country_code'] == $country_code && $_SESSION['forget_mobile'] == $mobile && $_SESSION['forget_mobile_expiretime'] > time()){
			return $this->fail('验证码5分钟有效，请勿多次发送!', 1002);
		}
		
		$limit = ip_limit();
		if($limit == 1){
			return $this->fail('您已当日发送次数过多!', 1003);
		}
		
		$mobile_code = random(6, 1);
		/* 发送验证码 */
		$result = sendCode($country_code, $mobile, $mobile_code);
		if($result['code'] == 0){
			$rs['verificationcode']               = $mobile_code;
			$_SESSION['forget_country_code']      = $country_code;
			$_SESSION['forget_mobile']            = $mobile;
			$_SESSION['forget_mobile_code']       = $mobile_code;
			$_SESSION['forget_mobile_expiretime'] = time() + 60 * 5;
		} elseif($result['code'] == 667){
			$_SESSION['forget_country_code']      = $country_code;
			$_SESSION['forget_mobile']            = $mobile;
			$_SESSION['forget_mobile_code']       = $result['msg'];
			$_SESSION['forget_mobile_expiretime'] = time() + 60 * 5;
			
			$rs['verificationcode'] = '123456';
			$rs['code']             = 1002;
			$rs['msg']              = '验证码为：'.$result['msg'];
		} else{
			$rs['code'] = 1002;
			$rs['msg']  = $result['msg'];
		}
		
		return $rs;
	}
	
	/**
	 * 获取微信登录unionid
	 * @desc 用于获取微信登录unionid
	 * @return int code 操作码，0表示成功,2发送失败
	 * @return array info
	 * @return string info[0].unionid 微信unionid
	 * @return string msg 提示信息
	 */
	public function getUnionid()
	{
		$code = checkNull($this->code);
		if($code == ''){
			return $this->fail('参数错误!', 1001);
		}
		
		$configpri = getConfigPri();
		$AppID     = $configpri['wx_mini_appid'];
		$AppSecret = $configpri['wx_mini_appsecret'];
		/* 获取token */
		//$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid={$AppID}&secret={$AppSecret}&code={$code}&grant_type=authorization_code";
		$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$AppID}&secret={$AppSecret}&js_code={$code}&grant_type=authorization_code";
		file_put_contents(API_ROOT.'/../log/phalapi/login_getUnionid_'.date('Y-m-d').'.txt', date('Y-m-d H:i:s').' 请求地址参数信息 url:'.$url."\r\n", FILE_APPEND);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_URL, $url);
		$json = curl_exec($ch);
		curl_close($ch);
		$arr = json_decode($json, 1);
		
		file_put_contents(API_ROOT.'/../log/phalapi/login_getUnionid_'.date('Y-m-d').'.txt', date('Y-m-d H:i:s').' 提交参数信息 code:'.json_encode($code)."\r\n", FILE_APPEND);
		file_put_contents(API_ROOT.'/../log/phalapi/login_getUnionid_'.date('Y-m-d').'.txt', date('Y-m-d H:i:s').' 返回参数信息 arr:'.json_encode($arr)."\r\n", FILE_APPEND);
		if($arr['errcode']){
			// file_put_contents(API_ROOT.'/../log/phalapi/login_getUnionid_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 返回错误信息 arr:'.json_encode($arr)."\r\n\r\n",FILE_APPEND);
			return $this->fail('小程序信息配置错误!', 1003);
		}
		
		// 小程序 绑定到 开放平台 才有 unionid  否则 用 openid
		$unionid = $arr['unionid'];
		if(!$unionid){
			//$rs['code']=1002;
			//$rs['msg']='公众号未绑定到开放平台';
			//return $rs;
			
			$unionid = $arr['openid'];
		}
		$info = ['unionid' => $unionid, 'openid' => $arr['openid']];
		
		return $this->success(['0' => $info]);
	}
	
	/**
	 * 退出
	 * @desc 用于用户退出 注销极光
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function logout()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		return $this->success();
	}
	
	
	/**
	 * 获取注销账号的条件
	 * @desc 用于获取注销账号的条件
	 * @return int code 状态码，0表示成功
	 * @return string msg 提示信息
	 * @return array info 返回信息
	 * @return array info[0]['list'] 条件数组
	 * @return string info[0]['list'][]['title'] 标题
	 * @return string info[0]['list'][]['content'] 内容
	 * @return string info[0]['list'][]['is_ok'] 是否满足条件 0 否 1 是
	 * @return string info[0]['can_cancel'] 是否可以注销账号 0 否 1 是
	 */
	public function getCancelCondition()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Login();
		$res    = $domain->getCancelCondition($uid);
		
		return $this->success([0 => $res]);
	}
	
	/**
	 * 用户注销账号
	 * @desc 用于用户注销账号
	 * @return int code 状态码,0表示成功
	 * @return string msg 返回提示信息
	 * @return array info 返回信息
	 */
	public function cancelAccount()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$time  = checkNull($this->time);
		$sign  = checkNull($this->sign);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		if(!$time || !$sign){
			return $this->fail('参数错误！', 1001);
		}
		
		$now = time();
		if($now - $time > 300){
			return $this->fail('参数错误！', 1001);
		}
		
		$checkdata = array('uid' => $uid, 'token' => $token, 'time' => $time);
		$issign    = checkSign($checkdata, $sign);
		if(!$issign){
			return $this->fail('签名错误！', 1001);
		}
		
		$domain = new Domain_Login();
		$res    = $domain->cancelAccount($uid);
		if($res == 1001){
			return $this->fail('相关内容不符合注销账号条件！', 1001);
		}
		
		return $this->success([], '注销成功,手机号、身份证号等信息已解除！');
	}
	
	/**
	 * 获取国家列表
	 * @desc 用于获取国家列表
	 * string field 搜索内容
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string name 国家中文名称
	 * @return string name_name 国家英文名称
	 * @return string tel 国家区号
	 * @return string msg 提示信息
	 */
	public function getCountrys()
	{
		$field = checkNull($this->field);
		$key   = 'getCountrys';
		$info  = getcaches($key);
		if(!$info){
			$country = API_ROOT.'/../data/config/country.json';
			// 从文件中读取数据到PHP变量
			$json_string = file_get_contents($country);
			// 用参数true把JSON字符串强制转成PHP数组
			$data = json_decode($json_string, true);
			$info = $data['country']; //国家
			
			setcaches($key, $info);
		}
		if($field){
			$info = $this->country_searchs($field, $info);
		}
		
		return $this->success($info);
	}
	
	private function country_searchs($field, $data)
	{
		$arr = array();
		foreach($data as $k => $v){
			$lists = $v['lists'];
			foreach($lists as $k => $v){
				//英文搜索替换为：$v['name_en']
				if(strstr($v['name'], $field) !== false){
					array_push($arr, $v);
				}
			}
		}
		
		return $arr;
	}
	
	/**
	 * 验证三方登录用户信息
	 * @param string $url api地址
	 * @param string $type api类型
	 * @return int code 状态码 0表示验证成功
	 * @return string msg 返回提示信息
	 * @return  code 状态码 0表示验证成功
	 * 微信:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Authorized_API_call_UnionID.html
	 * qq:https://wiki.connect.qq.com/%e5%bc%80%e5%8f%91%e6%94%bb%e7%95%a5_server-side
	 */
	private function checkThirdUserInfo($url, $type)
	{
		$rs     = array('code' => 0, 'msg' => '', 'info' => array());
		$header = array('Accept: application/json',);
		$curl   = curl_init();
		//设置抓取的url
		curl_setopt($curl, CURLOPT_URL, $url);
		//设置头文件的信息作为数据流输出
		curl_setopt($curl, CURLOPT_HEADER, 0);
		// 超时设置,以秒为单位
		curl_setopt($curl, CURLOPT_TIMEOUT, 1);
		
		// 超时设置，以毫秒为单位
		// curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
		
		// 设置请求头
		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		//设置获取的信息以文件流的形式返回，而不是直接输出。
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		//执行命令
		$result = curl_exec($curl);
		
		// 显示错误信息
		if(curl_error($curl)){
			$rs['code'] = 1001;
			$rs['msg']  = '信息验证失败';
			
		} else{
			// 打印返回的内容
			$res_arr = json_decode($result, true);
			if($type == 'wx'){
				//验证失败
				if(isset($res_arr['errcode'])){
					$rs['code'] = $res_arr['errcode'];
					$rs['msg']  = $res_arr['errmsg'];
				}
			} elseif($type == 'qq'){
				//验证失败
				if($res_arr['ret'] != 0){
					$rs['code'] = 50001;
					$rs['msg']  = $res_arr['msg'];
				}
			}
			curl_close($curl);
		}
		
		return $rs;
	}
	
	
	/**
	 * 检测短信开关
	 */
	private function checkSmsType($country_code, $mobile)
	{
		$configpri       = getConfigPri();
		$typecode_switch = $configpri['typecode_switch'];
		if($typecode_switch == 1){
			//阿里云验证码
			$aly_sendcode_type = $configpri['aly_sendcode_type'];
			if($aly_sendcode_type == 1){ //国内验证码
				if($country_code != 86){
					return $this->fail('平台只允许选择中国大陆！', 1001);
				}
				
				$ismobile = checkMobile($mobile);
				if(!$ismobile){
					return $this->fail('请输入正确的手机号！', 1001);
				}
			} elseif($aly_sendcode_type == 2){ //海外/港澳台 验证码
				if($country_code == 86){
					return $this->fail('平台只允许选择除中国大陆外的国家/地区！', 1001);
				}
			}
		} elseif($typecode_switch == 2){ //容联云
			$ismobile = checkMobile($mobile);
			if(!$ismobile){
				return $this->fail('请输入正确的手机号！', 1001);
			}
		} elseif($typecode_switch == 3){
			//腾讯云
			$tencent_sendcode_type = $configpri['tencent_sendcode_type'];
			if($tencent_sendcode_type == 1){ //中国大陆
				if($country_code != 86){
					return $this->fail('平台只允许选择中国大陆！', 1001);
				}
				
				$ismobile = checkMobile($mobile);
				if(!$ismobile){
					return $this->fail('请输入正确的手机号！', 1001);
				}
			} elseif($tencent_sendcode_type == 2){ //海外/港澳台 验证码
				if($country_code == 86){
					return $this->fail('平台只允许选择除中国大陆外的国家/地区！', 1001);
				}
			}
		} elseif($typecode_switch == 4){ //云之讯
			$ismobile = checkMobile($mobile);
			if(!$ismobile){
				return $this->fail('请输入正确的手机号！', 1001);
			}
		}
		
		return $this->success();
	}
	
}

